//
//  EAD_UserEditViewController.m
//  EpointFrame5
//
//  Created by AndLi on 14-6-20.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EAD_UserEditViewController.h"

@interface EAD_UserEditViewController ()

@end

@implementation EAD_UserEditViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if(IS_IPHONE_5){
            self = [super initWithNibName:@"EAD_UserEditViewController_5" bundle:nil];
        }else{
            self = [super initWithNibName:@"EAD_UserEditViewController" bundle:nil];
        }
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:UserGuid forKey:@"userguid"];
    [self startTask:@"EpointLoadUserHeadImageTask" withParams:params withTaskId:3];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UserGuid = [[NSString alloc]initWithString:[self.passParam objectForKey:@"userguid"]];
    userDetail = [DBFrameService getUserDetailByUserguid:UserGuid];
    
    
    [self.nameLabel setText:[userDetail objectForKey:@"displayname"]];
    [self.sexLabel setText:[userDetail objectForKey:@"sex"]];
    [self.deptLabel setText:[userDetail objectForKey:@"ouname"]];
    
    NSString *title = [userDetail objectForKey:@"title"];
    NSString *mobile = [userDetail objectForKey:@"mobile"];
    NSString *teleoffice = [userDetail objectForKey:@"teleoffice"];
    NSString *telehome = [userDetail objectForKey:@"telehome"];
    NSString *mail = [userDetail objectForKey:@"mail"];
    
    mobile = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    teleoffice = [teleoffice stringByReplacingOccurrencesOfString:@" " withString:@""];
    telehome = [telehome stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    
    
    if(title==nil || [title isEqual:@""]){
        [self.classLabel setText:@"职员"];
    }else{
        [self.classLabel setText:title];
    }
    
    
    [self.mobileTitleLabel setTextColor:UIColorFromRGB(0x0079fe)];
    [self.officeTitleLabel setTextColor:UIColorFromRGB(0x0079fe)];
    [self.familyTitleLabel setTextColor:UIColorFromRGB(0x0079fe)];
    [self.emailTitleLabel setTextColor:UIColorFromRGB(0x0079fe)];
    
    self.mobileTextField.text = mobile;
    self.officeTextField.text = teleoffice;
    self.familyTextField.text = telehome;
    self.emailTextField.text = mail;
    
    [self getUserHeadImage];
    
    [self initHJManage];
    CGFloat ivLength = 100;
    if (IS_IPHONE_5) {
        headImgView = [[HJManagedImageV alloc]initWithFrame:CGRectMake(20,42+35,ivLength,ivLength)];
    }else{
        headImgView = [[HJManagedImageV alloc]initWithFrame:CGRectMake(20,42-30+65,ivLength,ivLength)];
    }
    
    [self.mScrollView addSubview:headImgView];
    
    if([self.sexLabel.text isEqual:@"男"]){
        [headImgView setImage:[UIImage imageNamed:@"img_man.png"]];
    }else{
        [headImgView setImage:[UIImage imageNamed:@"img_woman.png"]];
    }
    
    [headImgView.imageView setContentMode:UIViewContentModeScaleToFill];
    headImgView.layer.masksToBounds = YES;
    headImgView.layer.cornerRadius = ivLength/2;
    headImgView.layer.borderWidth = 2;
    [headImgView.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    
    self.mScrollView.contentSize  = CGSizeMake(320, 700);
    self.mScrollView.bounces = NO;
    
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                      action:@selector(handleSingleFingerEvent:)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate= self;
    
    [headImgView setCallbackOnImageTap:self method:@selector(handleSingleFingerEvent:)];
}

#pragma mark -头像点击事件
- (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
{
    NSLog(@"click headimg");
//    NSString *name = (NSString *)[DBFrameService getUserDefaults:Epoint_User_DisplayName];
//    NSString *userguid = (NSString *)[DBFrameService getUserDefaults:Epoint_User_UserGuid];
//    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//    [params setObject:name forKey:@"title"];
//    [params setObject:userguid forKey:@"userguid"];
//    EAD_UserEditViewController *dv = [[EAD_UserEditViewController alloc]init];
//    dv.passParam = params;
//    [self.navigationController pushViewController:dv animated:YES];
}

#pragma mark 设置头像
-(void)setHeadImg{
    
    if(UserGuid!=nil && UserGuid.length>0){
        headImgView.url = [NSURL URLWithString:[EpointBizTools getUserHeadImageDataUrl:UserGuid]];
    }else{
        [headImgView setImage:[UIImage imageNamed:@"img_woman.png"]];
    }
    
    [objMan manage:headImgView];
    
}

//初始化图片缓存管理器
-(void)initHJManage
{
    objMan = [[HJObjManager alloc] initWithLoadingBufferSize:20 memCacheSize:30];
    NSString* cacheDirectory = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches/imgcache/flickr/"] ;
    HJMOFileCache* fileCache = [[HJMOFileCache alloc] initWithRootPath:cacheDirectory];
    objMan.fileCache = fileCache;
    fileCache.fileCountLimit = 100;
    fileCache.fileAgeLimit = 60*60*24*30; //1 week
    [fileCache trimCacheUsingBackgroundThread];
}


#pragma 获取用户头像
-(void)getUserHeadImage
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:[DBFrameService getUserGuid] forKey:[self.passParam objectForKey:@"userguid"]];
    [self startTask:@"EpointLoadUserHeadImageTask" withParams:params withTaskId:3];
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj
{
    if (taskId==3) {
        [self setHeadImg];
    }
}




@end
